﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Telerik.Web.UI;
using System.IO;

public partial class Documents_UploadDocuments_UploadDocument : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            this.ddTypes.SelectedValue = "Leg";
            this.ddProducts.DataSource = this.ldsLegs;
            this.ddProducts.DataTextField="NameBG";
            this.ddProducts.DataValueField = "LegID";
            this.ddProducts.DataBind();
        }
    }

    protected void ddTypes_IndexChanged(object sender, EventArgs e)
    {
        switch (this.ddTypes.SelectedValue)
        {
            case "Leg":
                this.ddProducts.DataSource = this.ldsLegs;
                this.ddProducts.DataTextField = "NameBG";
                this.ddProducts.DataValueField = "LegID";
                this.ddProducts.DataBind();
                break;
            case "Cover":
                this.ddProducts.DataSource = this.ldsCovers;
                this.ddProducts.DataTextField = "NameBG";
                this.ddProducts.DataValueField = "CoverID";
                this.ddProducts.DataBind();
                break;
            case "Material":
                this.ddProducts.DataSource = this.ldsMaterials;
                this.ddProducts.DataTextField = "Description";
                this.ddProducts.DataValueField = "MaterialID";
                this.ddProducts.DataBind();
                break;
            default: break;
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (this.iFile.UploadedFiles.Count > 0)
        {
            string fn;
            string saveLocation;
            try
            {
                foreach (UploadedFile f in this.iFile.UploadedFiles)
                {
                    fn = System.IO.Path.GetFileName(f.FileName);

                    switch (this.ddTypes.SelectedValue)
                    {
                        case "Leg":
                            saveLocation = Server.MapPath("Legs") + "\\" + fn;
                            break;
                        case "Cover":
                            saveLocation = Server.MapPath("Covers") + "\\" + fn;
                            break;
                        case "Material":
                            saveLocation = Server.MapPath("Materials") + "\\" + fn;
                            break;
                        default:
                            saveLocation = Server.MapPath("UploadedDocuments") + "\\" + fn;
                            break;
                    }

                    f.SaveAs(saveLocation);

                    SqlDataSource sdsUploadedFiles = new SqlDataSource();
                    sdsUploadedFiles.ConnectionString = ConfigurationManager.ConnectionStrings["KonturConnectionString"].ConnectionString;
                    sdsUploadedFiles.InsertParameters.Add(new Parameter("ProductID", TypeCode.Int32, this.ddProducts.SelectedValue));
                    sdsUploadedFiles.InsertParameters.Add(new Parameter("ProductType", TypeCode.String, this.ddTypes.SelectedValue));
                    sdsUploadedFiles.InsertParameters.Add(new Parameter("UploadedFilePath", TypeCode.String, saveLocation));
                    sdsUploadedFiles.InsertParameters.Add(new Parameter("ContentType", TypeCode.String, f.ContentType));
                    sdsUploadedFiles.InsertParameters.Add(new Parameter("ContentSize", TypeCode.Int64, f.ContentLength.ToString()));

                    sdsUploadedFiles.InsertCommand = "usp_UploadedFilesCreate";
                    sdsUploadedFiles.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
                    sdsUploadedFiles.Insert();


                }
            }
            catch (Exception ex)
            {
                this.lblResult.Text = "Съжаляваме, но възникна грешка! Моля опитайте отново!";
                this.lblResult.Text += ex.Message;
            }
            this.rgUploadedFiles.DataBind();
        }
        else
        {
            this.lblResult.Text = "Моля, изберете файл за прикачване!";
        }
       
    }

    protected void rgUploadedFiles_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            Label lblFile = (Label)dataItem.FindControl("lblFile");
            string text = lblFile.Text.Substring(lblFile.Text.LastIndexOf('\\')+1);
            lblFile.Text = text;
        }

    }

    protected void rgUploadedFiles_DeleteCommand(object source, GridCommandEventArgs e)
    {
        GridDataItem gdi = (GridDataItem)e.Item;
        string id = gdi.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UploadedFileID"].ToString();
        string filePath = gdi.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UploadedFilePath"].ToString();

        FileInfo fi = new FileInfo(filePath);
        fi.Delete();
    }
}
